package net.sppan.blog.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

	public static void main(String[] args) {
		System.out.println(daysBetween("2017-03-01", "2017-02-01"));
		System.out.println(getBetweenDates("2017-02-01", "2017-03-01"));

	}

	public static int daysBetween(String smdate, String bdate) {
		long between_days = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(smdate));
			long time1 = cal.getTimeInMillis();
			cal.setTime(sdf.parse(bdate));
			long time2 = cal.getTimeInMillis();
			between_days = (time2 - time1) / (1000 * 3600 * 24);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Integer.parseInt(String.valueOf(between_days));
	}

	public static List<String> getBetweenDates(String start, String end) {
		List<String> result = new ArrayList<String>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.setTime(sdf.parse(start));
			Date start_ = cal.getTime();
			cal.setTime(sdf.parse(end));
			Date end_ = cal.getTime();

			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start_);
			tempStart.add(Calendar.DAY_OF_YEAR, 0);
			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end_);
			tempEnd.add(Calendar.DAY_OF_YEAR, 1);
			while (tempStart.before(tempEnd)) {
				result.add(sdf.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
